package asteroids.model.programs;

import java.util.Map;
import asteroids.*;
import asteroids.statement.*;
import asteroids.expression.*;

public class Program {

	private Ship ship;
	private Statement statement;
	private Map<String, Expression> globals;

	public Program(Map<String, Expression> globals, Statement statement) {
		if (statement == null) {
			throw new NullPointerException(
					"You cannot construct a program with null as a statement.");
		}
		this.statement = statement;
		this.globals = globals;
	}

	public Program(Map<String, Expression> globals, Statement statement,
			Ship ship) {

		this(globals, statement);
		loadOnToShip(ship);

	}

	public void execute() {
		if (!statement.isFinished()) {
			statement.execute(this);
		}
	}

	public void loadOnToShip(Ship ship) {
		if (ship != null)
			this.ship = ship;
	}

	public Map<String, Expression> getGlobals() {
		return this.globals;
	}

	public void setGlobals(String key, Expression newValue) {
		this.globals.put(key, newValue);
	}

	public Ship getOwner() {
		return this.ship;
	}

}
